<template>
  <div>
    <el-table
      :data="specslist"
      style="width: 100%"
      row-key="id"
      border
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="id" label="规格编号"> </el-table-column>
      <el-table-column prop="specsname" label="规格名称"> </el-table-column>
      <el-table-column label="规格属性">
        <template slot-scope="scope">
          <div>
            <el-tag v-for="(item, index) in scope.row.attrs" :key="index">{{
              item
            }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div>
            <el-button type="info" v-if="scope.row.status == 1">启用</el-button>
            <el-button type="warning" v-else>禁用</el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div>
            <el-button type="success" @click="bianji(scope.row.id)">编辑</el-button>
            <v-del @del='del(scope.row.id)'></v-del>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination background layout="prev, pager, next" :total="specscount" :page-size="specssize" @current-change="changepage">
    </el-pagination>
  </div>
</template>


<script>
import { mapActions, mapGetters } from "vuex";
import {specsdelete} from '../../../request/api'
import { successAlert, warningAlert } from "../../../util/alert";
export default {
  data() {
    return {

    };
  },
  methods: {
    ...mapActions({
      reqspecslist: "specs/reqchangelist",
      reqspecscount:'specs/reqchangecount',
      reqspecspage:'specs/reqchangepage'
    }),
    // 页数改变时
    changepage(num){
      this.reqspecspage(num)
    },
    // 点击删除
    del(id){
      specsdelete({id:id}).then(res=>{
        if (res.data.code == 200) {
          // 删除成功弹窗
          successAlert(res.data.msg);
          // 重新请求列表数据
          this.reqspecslist();
          // 重新请求总条数
          this.reqspecscount()
        } else {
          // 删除失败弹窗
          warningAlert(res.data.msg);
        }
      })
    },
    // 点击编辑
    bianji(id){
      this.$emit('bianji',id)
    }
  },
  computed: {
    ...mapGetters({
      specslist: "specs/list",
      specscount:'specs/count',
      specssize:'specs/size'
    }),
  },
  mounted() {
    this.reqspecslist();
    this.reqspecscount()
    this.reqspecspage(1)
  },
};
</script>

<style>
</style>